package com.wuxianedu.bitmapdrawable;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
//        testDraw();
       // testBMP();
        bmp2Draw();
    }



    public void testDraw() {
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        iv.setImageDrawable(drawable);
    }

    public void testBMP(){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        iv.setImageBitmap(bitmap);


    }

    /**
     * drawable -----> bitnmap
     */
    public void draw2BMP(){
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        iv.setImageBitmap(bitmap);
    }

    /**
     * bitmap----->drawable
     */
    public void bmp2Draw(){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Drawable drawable = new BitmapDrawable(bitmap);
        iv.setImageDrawable(drawable);
    }



}
